package com.noble.meether.logic.common.bean;

import android.os.Parcel;
import android.os.Parcelable;

import com.noble.library.base.bean.BaseData;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 升级更新
 * Created by Su on 2017/12/4.
 */
public class Updata extends BaseData implements Parcelable {
    private long verCode;    // 版本号： 不通过它做升级判断
    private String appUrl;      // 更新地址
    private boolean mustUpdate;     // 是否强制升级
    private boolean upgrade;    // 是否需要升级，判断升级的唯一条件，代替verCode判断

    private List<String> explainList = new ArrayList<>();

    @Override
    public void parseJson(String jsonStr) {
        JSONObject jsonObject = getJsonObject(jsonStr);

        this.verCode = jsonObject.optLong("Version");
        this.appUrl = jsonObject.optString("Url");
        this.mustUpdate = jsonObject.optBoolean("Must");
        this.upgrade = jsonObject.optBoolean("Upgrade");

        if (!jsonObject.isNull("Content")) {
            String json = jsonObject.optString("Content");
            JSONArray jsonArray = getJsonArray(json);
            this.explainList = getStringList(jsonArray);
        }
    }

    public boolean isUpgrade() {
        return upgrade;
    }

    public boolean isMustUpdate() {
        return mustUpdate;
    }

    public long getVerCode() {
        return verCode;
    }

    public String getAppUrl() {
        return appUrl;
    }

    public List<String> getExplainList() {
        return explainList;
    }

    public Updata() {
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(this.verCode);
        dest.writeString(this.appUrl);
        dest.writeByte(this.mustUpdate ? (byte) 1 : (byte) 0);
        dest.writeByte(this.upgrade ? (byte) 1 : (byte) 0);
        dest.writeStringList(this.explainList);
    }

    protected Updata(Parcel in) {
        this.verCode = in.readLong();
        this.appUrl = in.readString();
        this.mustUpdate = in.readByte() != 0;
        this.upgrade = in.readByte() != 0;
        this.explainList = in.createStringArrayList();
    }

    public static final Creator<Updata> CREATOR = new Creator<Updata>() {
        @Override
        public Updata createFromParcel(Parcel source) {
            return new Updata(source);
        }

        @Override
        public Updata[] newArray(int size) {
            return new Updata[size];
        }
    };
}
